/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Collir.java
 *
 * Created on 15/03/2011, 12:06:31
 */
package vistas;

import Renderers.LabelRenderer;
import comu.Utilitats;
import controlador.ProduccioControlador;
import controlador.ProducteControlador;
import controlador.UnitatsMesuraControlador;
import controlador.autogenerats.exceptions.IllegalOrphanException;
import controlador.autogenerats.exceptions.NonexistentEntityException;
import entity.Camp;
import entity.Produccio;
import entity.Producte;
import entity.UnitatsMesura;
import java.io.IOException;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import vistas.dialogs.DialogSituarParcela;
import vistas.textImg.Textos;

/**
 *
 * @author David
 */
public final class Plantar extends javax.swing.JFrame implements Observer {

    JFrame menu;
    Produccio produccio;
    Camp camp;
    ProducteControlador productes;
    ProduccioControlador produccions;
    UnitatsMesuraControlador unitatsMesura;
    DialogSituarParcela situarParcela;

    /** Creates new form Collir */
    Plantar(JFrame menu, Produccio produccio, Camp camp) {
        this.menu = menu;
        this.camp = camp;
        this.produccio = produccio;

        unitatsMesura = new UnitatsMesuraControlador();
        produccions = new ProduccioControlador();
        productes = new ProducteControlador();

        initComponents();
        llistaProductes.setCellRenderer(new LabelRenderer());
        Utilitats.centrarVentana(this);
        try {
            if (this.camp != null) {
                update(null, this.camp);
            } else if (this.produccio != null) {
                update(null, this.produccio);
            }
//            if (this.produccio != null) {
//                llistaProductes.setSelectedValue(produccio.getProductesId(), true);
//                ftfData.setText(Configuracio.sdf_ddMMyyyy.format(produccio.getDataPlanta()));
//                ftfTotal.setText(String.valueOf(produccio.getQuantitat()));
//                List<Produccio> veins = produccions.findProduccioEntitiesByCamp(this.camp, false);
//                veins.remove(produccio);
//                situarParcela = new DialogSituarParcela(this, true, this.camp, produccio, veins, true);
//                tfVarietat.setText(produccio.getVarietat());
//                tfOrigen.setText(produccio.getOrigen());
//            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        llistaProductes = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        btAvui = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        cbUnitat = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        btCancelar = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        ftfTotal = new javax.swing.JFormattedTextField();
        ftfCadaCaixa = new javax.swing.JFormattedTextField();
        jLabel4 = new javax.swing.JLabel();
        ftfCaixes = new javax.swing.JFormattedTextField();
        btAceptar = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        tfOrigen = new javax.swing.JTextField();
        tfVarietat = new javax.swing.JTextField();
        btMenu = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        lblMapaCamp = new javax.swing.JLabel();
        dcDataPlantacio = new com.toedter.calendar.JDateChooser();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        llistaProductes.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        llistaProductes.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                llistaProductesValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(llistaProductes);

        jLabel3.setText(Textos.PLANTAR_LABEL_DATA);

        btAvui.setText(Textos.PLANTAR_BOTO_AVUI);
        btAvui.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAvuiActionPerformed(evt);
            }
        });

        jLabel2.setText(Textos.PLANTAR_LABEL_UNITATMESURA);

        cbUnitat.setModel(Utilitats.getComboBoxModel(unitatsMesura.findUnitatsMesuraEntitiesForPlantar()));
        cbUnitat.setAutoscrolls(true);
        cbUnitat.setRenderer(new LabelRenderer());
        cbUnitat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbUnitatActionPerformed(evt);
            }
        });

        jLabel1.setText(Textos.PLANTAR_LABEL_QUANTITAT);

        btCancelar.setText(Textos.COMU_BOTO_CANCELAR);
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        jLabel5.setText(Textos.PLANTAR_LABEL_PER_CADA);

        ftfTotal.setText("0");
        ftfTotal.setFont(Textos.FONT_GRAN);

        ftfCadaCaixa.setText("0");
        ftfCadaCaixa.setFont(Textos.FONT_GRAN);
        ftfCadaCaixa.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                ftfCaixesKeyTyped(evt);
            }
        });

        jLabel4.setText(Textos.PLANTAR_LABEL_CAIXES);

        ftfCaixes.setText("0");
        ftfCaixes.setFont(Textos.FONT_GRAN);
        ftfCaixes.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                ftfCaixesKeyTyped(evt);
            }
        });

        btAceptar.setText(Textos.COMU_BOTO_ACCEPTAR);
        btAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAceptarActionPerformed(evt);
            }
        });

        jLabel6.setText(Textos.PLANTAR_LABEL_ORIGEN);

        jLabel7.setText(Textos.PLANTAR_LABEL_VARIETAT);

        tfOrigen.setFont(Textos.FONT_GRAN);

        tfVarietat.setFont(Textos.FONT_GRAN);

        btMenu.setText(Textos.COMU_BOTO_MENU);
        btMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btMenuActionPerformed(evt);
            }
        });

        jLabel8.setText(vistas.textImg.Textos.PLANTAR_LABEL_PRODUCTES);

        lblMapaCamp.setText(" ");
        lblMapaCamp.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblMapaCampMouseClicked(evt);
            }
        });

        dcDataPlantacio.setDate(GregorianCalendar.getInstance().getTime());

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 245, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel8, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 180, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(jLabel7)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(tfVarietat, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(jLabel6)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(tfOrigen, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(ftfTotal, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(jLabel5)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(ftfCadaCaixa, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(jLabel4)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(ftfCaixes, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(jLabel3)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(dcDataPlantacio, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 162, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btAvui, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 97, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(lblMapaCamp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 221, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, btAceptar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, btMenu, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(btCancelar)))
                    .add(layout.createSequentialGroup()
                        .add(jLabel2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cbUnitat, 0, 271, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(dcDataPlantacio, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                .add(jLabel3)
                                .add(btAvui, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel2)
                            .add(cbUnitat, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel4)
                            .add(ftfCaixes, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel5)
                            .add(ftfCadaCaixa, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 38, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(ftfTotal, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel1))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(tfOrigen, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel6))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(tfVarietat, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel7))
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(btAceptar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 71, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 25, Short.MAX_VALUE)
                                .add(btCancelar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 71, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(19, 19, 19)
                                .add(btMenu, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(lblMapaCamp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 182, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(34, 34, 34))))
                    .add(layout.createSequentialGroup()
                        .add(8, 8, 8)
                        .add(jLabel8)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 552, Short.MAX_VALUE)))
                .add(20, 20, 20))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed

        ftfCadaCaixa.setText("0");
        ftfCaixes.setText("0");
        ftfTotal.setText("0");
        tfOrigen.setText("");
        tfVarietat.setText("");
        dcDataPlantacio.setDate(GregorianCalendar.getInstance().getTime());
        if (produccio != null) {
            produccio = null;
            btMenuActionPerformed(evt);
        } else {
            produccio = null;
        }
    }//GEN-LAST:event_btCancelarActionPerformed

    private void btAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAceptarActionPerformed

        Producte producte = (Producte) llistaProductes.getSelectedValue();
        if (producte != null) {
            if (camp != null) {
                try {
                    Date data = dcDataPlantacio.getDate();
                     Integer quantitat = Integer.valueOf(ftfTotal.getText());
                    if (quantitat != 0) {
                        UnitatsMesura unitat = (UnitatsMesura) cbUnitat.getSelectedItem();
                        if (unitat != null) {
                            if (situarParcela == null) {
                                situarParcela = new DialogSituarParcela(this, true, camp, null, null, true);
                            }
                            if (produccio == null) {
                                produccio = new Produccio(null);
                                produccio.setDataPlanta(data);
                                produccio.setQuantitat(quantitat);
                                produccio.setTerminado(false);
                                produccio.setOrigen(tfOrigen.getText());
                                produccio.setVarietat(tfVarietat.getText());
                                produccio.setPosx1(situarParcela.getX1());
                                produccio.setPosx2(situarParcela.getX2());
                                produccio.setPosy1(situarParcela.getY1());
                                produccio.setPosy2(situarParcela.getY2());
                                produccio.setProductesId(producte);
                                produccio.setCampId(camp);
                                produccio.setUnitatsMesuraId(unitat);
                                produccions.create(produccio);
                                btCancelarActionPerformed(evt);
                                update(null, null);
                            } else {
                                produccio.setDataPlanta(data);
                                produccio.setQuantitat(quantitat);
                                produccio.setTerminado(false);
                                produccio.setOrigen(tfOrigen.getText());
                                produccio.setVarietat(tfVarietat.getText());
                                produccio.setPosx1(situarParcela.getX1());
                                produccio.setPosx2(situarParcela.getX2());
                                produccio.setPosy1(situarParcela.getY1());
                                produccio.setPosy2(situarParcela.getY2());
                                produccio.setProductesId(producte);
                                produccio.setCampId(camp);
                                produccio.setUnitatsMesuraId(unitat);
                                produccions.edit(produccio);
                                btCancelarActionPerformed(evt);
                                Thread.sleep(200);
                                btMenuActionPerformed(evt);
                            }
                        } else {
                            JOptionPane.showMessageDialog(this, Textos.PLANTAR_INFO_PRODUCCIO_UNITAT, "", JOptionPane.WARNING_MESSAGE);
                        }
                    } else {
                        JOptionPane.showMessageDialog(this, Textos.PLANTAR_INFO_PRODUCCIO_QUANTITAT, "", JOptionPane.WARNING_MESSAGE);
                    }
                } catch (IllegalOrphanException ex) {
                    Logger.getLogger(Plantar.class.getName()).log(Level.SEVERE, null, ex);
                } catch (NonexistentEntityException ex) {
                    Logger.getLogger(Plantar.class.getName()).log(Level.SEVERE, null, ex);
                } catch (Exception ex) {
                    Logger.getLogger(Plantar.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                JOptionPane.showMessageDialog(this, Textos.PLANTAR_INFO_PRODUCCIO_CAMP, "", JOptionPane.WARNING_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(this, Textos.PLANTAR_INFO_PRODUCCIO_PRODUCTE, "", JOptionPane.WARNING_MESSAGE);
        }
}//GEN-LAST:event_btAceptarActionPerformed

    private void cbUnitatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbUnitatActionPerformed
        UnitatsMesura unitatMesura = (UnitatsMesura) cbUnitat.getSelectedItem();
        if (unitatMesura.getUnitatsMesuraId() != null) {
            jLabel4.setText(unitatMesura.getUnitatsMesuraId().getNom());
        }
    }//GEN-LAST:event_cbUnitatActionPerformed

    private void btMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btMenuActionPerformed
        if (menu != null) {
            if (General.class.isInstance(menu)) {
                ((General) menu).update(null, null);
            }
            menu.setVisible(true);
            this.setVisible(false);
        }
    }//GEN-LAST:event_btMenuActionPerformed

    private void ftfCaixesKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_ftfCaixesKeyTyped
        if ((!ftfCaixes.getText().equals("0")) && (!ftfCadaCaixa.getText().equals("0"))) {
            try {
                ftfTotal.setText(String.valueOf(Integer.parseInt(ftfCadaCaixa.getText()) * Integer.parseInt(ftfCaixes.getText())));
            } catch (Exception e) {
                ftfTotal.setText("0");
            }
        }
    }//GEN-LAST:event_ftfCaixesKeyTyped

    private void llistaProductesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_llistaProductesValueChanged
        if (produccio != null) {
            Producte producte = (Producte) llistaProductes.getSelectedValue();
            if (producte != null) {
                produccio.setProductesId(producte);
            }
        }
    }//GEN-LAST:event_llistaProductesValueChanged

    private void lblMapaCampMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblMapaCampMouseClicked
        if (situarParcela == null) {
            List<Produccio> veins = produccions.findProduccioEntitiesByCamp(this.camp, false);
            veins.remove(produccio);
            situarParcela = new DialogSituarParcela(this, true, this.camp, produccio, veins, true);
        }
        situarParcela.setVisible(true);
    }//GEN-LAST:event_lblMapaCampMouseClicked

    private void btAvuiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAvuiActionPerformed
        dcDataPlantacio.setDate(GregorianCalendar.getInstance().getTime());
}//GEN-LAST:event_btAvuiActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAceptar;
    private javax.swing.JButton btAvui;
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btMenu;
    private javax.swing.JComboBox cbUnitat;
    private com.toedter.calendar.JDateChooser dcDataPlantacio;
    private javax.swing.JFormattedTextField ftfCadaCaixa;
    private javax.swing.JFormattedTextField ftfCaixes;
    private javax.swing.JFormattedTextField ftfTotal;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblMapaCamp;
    private javax.swing.JList llistaProductes;
    private javax.swing.JTextField tfOrigen;
    private javax.swing.JTextField tfVarietat;
    // End of variables declaration//GEN-END:variables

    public void update(Observable o, Object o1) {
        if (Camp.class.isInstance(o1)) {
            try {
                produccio = null;
                camp = (Camp) o1;
                Utilitats.adabtarImatgeAlLabel(lblMapaCamp, camp.getImg());
                update_Productes();
                btCancelarActionPerformed(null);
            } catch (IOException ex) {
                Logger.getLogger(Plantar.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else if (Produccio.class.isInstance(o1)) {
            try {
                produccio = (Produccio) o1;
                camp = produccio.getCampId();
                Utilitats.adabtarImatgeAlLabel(lblMapaCamp, camp.getImg());
                update_Productes();
                List<Produccio> veins = produccions.findProduccioEntitiesByCamp(camp, false);
                veins.remove(produccio);
                situarParcela = new DialogSituarParcela(this, true, camp, produccio, veins, true);
                llistaProductes.setSelectedValue(produccio.getProductesId(), true);
                dcDataPlantacio.setDate(produccio.getDataPlanta());
                cbUnitat.setSelectedItem(produccio.getUnitatsMesuraId());
                ftfTotal.setText(String.valueOf(produccio.getQuantitat()));
                tfOrigen.setText(produccio.getOrigen());
                tfVarietat.setText(produccio.getVarietat());
            } catch (IOException ex) {
                Logger.getLogger(Plantar.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            Producte selectedProducte = (Producte) llistaProductes.getSelectedValue();
            update_Productes();
            cbUnitatActionPerformed(null);
            if (selectedProducte != null) {
                llistaProductes.setSelectedValue(selectedProducte, true);
            }
        }
    }

    private void update_Productes() {
        llistaProductes.setModel(getListModel(productes.findProducteEntitiesAZ()));
    }

    private DefaultListModel getListModel(List llista) {
        DefaultListModel modelP = new DefaultListModel();
        for (int i = 0; i < llista.size(); i++) {
            modelP.addElement(llista.get(i));
        }
        return modelP;
    }
}
